package io.modifier.basic.emo.core

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Build

fun Context.currentProcessName(): String {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        val processName = Application.getProcessName()
        if (processName != null && processName.isNotBlank()) {
            return processName
        }
    }

    val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
    val infoList = activityManager.runningAppProcesses
    return infoList?.find { it.pid == android.os.Process.myPid() }?.processName ?: packageName
}

fun Context.currentSimpleProcessName(): String {
    val name = currentProcessName()
    return name.split(":").getOrElse(1) { "main" }
}
